class Solution {
public:
    int majorityElement(vector<int>& nums) {
        int i = 0;
        int j = i + 1;
        int count = 1;
        std::sort(nums.begin(), nums.end());
        if (nums.size() == 1)
        {
            return nums[0];
        }
        while (j < nums.size())
        {
            if (nums[i] == nums[j])
            {
                count++;
                j++;
            }
            else
            {
                if (count <= (nums.size() / 2))
                {
                    count = 1;
                    j++;
                }
                else {
                    return nums[i];
                }
                i = j - 1;
            }
            if (j == nums.size())
            {
                return nums[i];
            }
        }
        return nums[i];
    }
};